package com.hardydou.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hardydou.common.result.Result;
import com.hardydou.common.constant.Const.Status;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.framework.log.anno.LogMetaInfo;
import com.hardydou.system.model.SystemRole;
import com.hardydou.system.service.ISystemResService;
import com.hardydou.system.service.ISystemRoleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：
 * @date ：Created in 2020-03-10
 * @Copyright ：Hardydou.com
 */
@Controller
@RequestMapping("/system/role")
@Api(tags = "角色管理")
@LogMetaInfo(module = "角色管理")
public class SysRoleController extends BaseController {

    @Autowired
    ISystemRoleService systemRoleService;
    ISystemResService resService;

    @ApiIgnore
    @GetMapping("")
    public String index() {
        return "role/list";
    }


    @ApiIgnore
    @GetMapping("/add")
    public String add() {
        return "role/add";
    }


    @ApiIgnore
    @PostMapping("/add")
    public String add(SystemRole role, Model model) {
        systemRoleService.addRole(role);
        model.addAttribute("code", 200);
        model.addAttribute("message", "添加成功");
        return "role/add";
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(long roleId, Model model) {
        SystemRole role = systemRoleService.findById(roleId);
        model.addAttribute("role", role);
        return "role/edit";
    }

    @ApiIgnore
    @PostMapping("/edit")
    public String edit(SystemRole role, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        systemRoleService.update(role);
        model.addAttribute("role", role);
        return "role/edit";
    }


    @ApiIgnore
    @GetMapping("/info")
    public String info(long roleId, Model model) {
        SystemRole role = systemRoleService.findById(roleId);
        model.addAttribute("role", role);
        return "role/info";
    }


    /* JSON API */

    @ApiOperation(value = "角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listForJson(@RequestParam Map params) {
        PageHelper.startPage(getRequest());
        List<SystemRole> roles = systemRoleService.findAll(params);
        PageInfo pageInfo = new PageInfo(roles);
        return ResultHelper.ok(pageInfo);
    }


    @ApiOperation(value = "添加角色")
    @PostMapping("/add.json")
    @ResponseBody
    public Result<?> addForJson(SystemRole role) {
        systemRoleService.addRole(role);
        return ResultHelper.ok(role);
    }


    @ApiOperation(value = "删除角色")
    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    @DeleteMapping("/del")
    @ResponseBody
    public Result delete(long roleId) {
        systemRoleService.deleteByRoleId(roleId);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("roleId", roleId);
        return result;
    }

    @ApiOperation(value = "更新角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "query")
    })
    @ApiParam(name = "roleId", value = "角色ID", required = true)
    @PostMapping("/update")
    @ResponseBody
    public Result update(SystemRole role) {
        systemRoleService.update(role);
        return ResultHelper.ok(role);
    }

    @ApiOperation(value = "更新状态")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    )
    @PostMapping("/status")
    @ResponseBody
    public Result status(long roleId) {
        SystemRole role = systemRoleService.findById(roleId);
        role.setStatus(role.getStatus() == Status.NORMAL.val ? Status.LOCKED.val : Status.NORMAL.val);
        systemRoleService.update(role);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("roleId", roleId);
        result.getData().put("status", role.getStatus());
        return result;
    }

}
